# -*- coding: utf-8 -*-
"""
Created on Sun Aug 15 15:52:11 2021

@author: 23119
"""

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

In_Pin=12#操控线（黄线）
#VCC接在5V脚上，GND接在GND脚上
GPIO.setup(In_Pin,GPIO.OUT,initial=GPIO.LOW)
p=GPIO.PWM(In_Pin,50)#设置频率为50KHz
p.start(0)
str1="please input the degree(0<=a<=120)\nor press q to quit\n"
r=input(str1)
try:
    while not r=="q":
        if r.isdigit():#判断输入的字符串是不是数字
            r=int(r)#是数字转换成数字
        else:
            print("please input a number(0<=num<=180)")
            continue
        if r<0 or r>180:#越界提示
            print("a must be [0,180]")
            continue
        p.ChangeDutyCycle(2.5+r/360*20)#通过用户输入的角度来改变舵机的角度
        time.sleep(0.02)
        r=str(input(str1))
except KeyboardInterrupt:
    pass
p.stop()
GPIO.cleanup()
